
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;

ENTITY fs_reed_contact IS
  PORT( 
  delta_t             : IN    NATURAL RANGE 0 TO 65535 := 0;  -- 0.1 ms
  speed               : IN    NATURAL RANGE 0 TO   999 := 0;  -- hm/h
  wheel_size          : IN    NATURAL RANGE 0 TO    28 := 0;  -- inch
  REED_contact        : OUT   STD_LOGIC  := '1'
  );
END ENTITY fs_reed_contact;

-------------------------------------------
-------------------------------------------

ARCHITECTURE v OF fs_reed_contact IS


SIGNAL clk   : STD_LOGIC := '0';

  SIGNAL timer :  NATURAL RANGE 0 TO 65535 := 0;
    
  SIGNAL dT    :  NATURAL RANGE 0 TO 65535 := 1;


BEGIN

clk <= NOT clk AFTER 50 us;
  
  calc_dT:
    PROCESS(delta_t, speed)
  
  
    VARIABLE circumference : REAL;
    VARIABLE m_per_sec     : REAL;
    
    
  BEGIN
  
    IF speed > 0 THEN
      circumference := (3.14 * 25.4 * REAL(CONV_INTEGER(wheel_size)));
      m_per_sec     := REAL(CONV_INTEGER(speed))  * 1.0 / 36.0 ;
      dT            <= INTEGER(circumference * 10.0 / m_per_sec) ;
    ELSE
      dT           <= CONV_INTEGER(delta_t);
    END IF;
      
  END PROCESS;
  
  
  
  Pulse:
  PROCESS(clk)
    VARIABLE dT_10 : NATURAL;
    BEGIN
   
    IF RISING_EDGE(clk) THEN
    
      IF timer >= dT-1 THEN
        timer <= 0;
      ELSE
        dT_10 := dT / 10;
      
        IF timer = 0 THEN
           REED_contact <= '0';
        ELSIF timer = 15 THEN
             REED_contact <= '1';
        ELSIF timer = 30 THEN
            REED_contact <= '0';
        ELSIF timer = dT_10 THEN
            REED_contact <= '1';
        ELSIF timer = dT_10+15 THEN
            REED_contact <= '0';
        ELSIF timer = dT_10+30 THEN
            REED_contact <= '1';
        END IF;

        timer <= timer + 1;
      END IF;

      
    END IF;
  END PROCESS; 
  

  
  
          
       
END ARCHITECTURE v;

